package com.pzhu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pzhu.common.constant.Constant;
import com.pzhu.common.constant.ErrorCode;
import com.pzhu.model.entity.Menu;
import com.pzhu.model.help.ResponseResult;
import com.pzhu.service.MenuService;
import com.pzhu.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : zqh
 * @Date : 2023/11/26 0026 下午 5:12
 * @Version: 1.0
 */
@RestController
@Api(tags = "系统菜单")
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation("菜单列表(树形展示)")
    @RequestMapping("/list")
    @PreAuthorize("hasAuthority('system:menu:query')")
    public ResponseResult list(){
        // 查询所有菜单信息
        List<Menu> menuList=menuService.list(new LambdaQueryWrapper<Menu>()
                .orderByAsc(Menu::getOrderNum));
        return ResponseResult.ok().put("treeMenu",menuService.buildTreeMenu(menuList));
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @ApiOperation("根据ID查询菜单信息")
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('system:menu:query')")
    @ApiImplicitParam(name = "id",value = "菜单ID",required = true)
    public ResponseResult findById(@PathVariable(value = "id")  Long id){
        Menu menu = menuService.getById(id);
        Map<String,Object> map=new HashMap<>();
        map.put("sysMenu",menu);
        return ResponseResult.ok(map);
    }

    /**
     * 添加或者修改
     * @param menu
     * @return
     */
    @ApiOperation("添加或修改菜单信息")
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('system:menu:add')"+"||"+"hasAuthority('system:menu:edit')")
    public ResponseResult save(@RequestBody @Valid Menu menu){
        if(menu.getId()==null || menu.getId()==-1){
            menu.setCreateTime(new Date());
            menuService.save(menu);
        }else{
            menu.setUpdateTime(new Date());
            menuService.updateById(menu);
        }
        redisUtil.removeByPrex(Constant.AUTHORITY_KEY);
        return ResponseResult.ok();
    }


    /**
     * 删除
     * 首先判断是否有子菜单
     * @param id
     * @return
     */
    @ApiOperation("根据id删除菜单信息")
    @GetMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('system:menu:delete')")
    @ApiImplicitParam(name = "id",value = "菜单ID",required = true)
    public ResponseResult delete(@PathVariable(value = "id")Long id){
        int count = menuService.count(new LambdaQueryWrapper<Menu>().eq(Menu::getParentId, id));
        if(count>0){
            return ResponseResult.error(ErrorCode.NEED_DEL_CHILDREN);
        }
        menuService.removeById(id);
        redisUtil.removeByPrex(Constant.AUTHORITY_KEY);
        return ResponseResult.ok();
    }

}

